<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <title></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp" %>
    <!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1>未申报药品</h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i>首页</a></li>
			<li class="active"><a href="#">采购计划管理</a></li>
            <li class="active"><a href="#">申报采购计划</a></li>
        </ol>
    </section>
    
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
	            <div class="input">
	            	<div class="textalign1">药品编码：</div>
	            	<div class="boxinput">
	            		<input class="textinput" type="text" id="drugCode" name="drugCode" placeholder="药品编码" />
	            	</div>
	            	<div class="textalign1">通用名：</div>
	            	<div class="boxinput">
	            		<input class="textinput" type="text" id="drugName" name="drugName" placeholder="通用名" />
	            	</div>
	            	<div class="textalign1">生产企业：</div>
	            	<div class="boxinput">
	            		<input class="textinput"  type="text" id="compName" name="compName" placeholder="生产企业" />
	            	</div>
	            	<div class="textalign1">
	            		<button type="button" onclick="gridSerch()" class="btn btn-success btn-sm" id="selectc">查&nbsp;询</button>
	            	</div>
	            </div>
            </div>
        </div>

        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>

        <div class=" box box-warning bottongroup">
        	<a href="javascript:void(0)" onclick="batchDeclareDrug()" class="btn btn-default btn-sm">提&nbsp;交</a>
            <a href="${ctx }/wsjplan/toDeclareDrugList.html?projId=${param.projId}" class="btn btn-default btn-sm">查看已申报药品</a>
            <a href="${ctx }/wsjplan/toWsjPlanProjectList.html" class="btn btn-default btn-sm">返&nbsp; 回</a>
        </div>
        
        
    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    <script type="text/javascript">
	    
	  //绑定回车
	    $('#drugCode,#drugName,#compName').bind('keydown', function(event) {
	        if (event.keyCode == "13") {        //13 位电脑上回车键的位置代码
	            $('#selectc').click();
	        }
	    }); 
			
	    $("#drugCode").focus();//加载页面光标在第一个查询框内
	    
        $(function () {
            $("#gridlist").jqGrid({
                url: "${pageContext.request.contextPath}/wsjplan/getNoDeclareDrugList.html?projId=${param.projId}&healbureaId=${healbureaId}",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit: false,
                height: 255,
                colNames: ['projId','drugid', '药品编码', '通用名', '剂型', '规格', '转换系数',"单位","材质", '生产企业', '去年采购数量','计划采购数量','采购方式','质量要求'],
                colModel: [
						{ name: 'projId', hidden:true,width: 40, align: 'center' ,sortable: false},
						{ name: 'drugId',hidden:true, width: 80, align: 'center' ,sortable: false,
							formatter:function(cellvalue, options, row){ 
								return row.drugCode;
							}
						},
						{ name: 'drugCode', width: 100 , align:"left",sortable: false,
							formatter:function(cellvalue, options, row){ 
								var str = "<a href=\"javascript:void(0)\" onclick=\"showDrugInfo('"+row.drugCode+"','"+row.drugName+"')\">"+cellvalue+"</a>";
								return str;
							}
						},
						{ name: 'drugName', width: 120, align: 'center' ,sortable: false},
						{ name: 'formName', width: 60, align: 'center' ,sortable: false},
						{ name: 'specName', width: 80, align: 'center' ,sortable: false},
						{ name: 'factor', width: 70, align: 'center' ,sortable: false},
						{ name: 'unit', width: 40, align: 'center' ,sortable: false},
						{ name: 'materialName', width: 60, align: 'center' ,sortable: false},
						{ name: 'prodPompName', width: 200, align: 'center' ,sortable: false,
							formatter:function(cellvalue, options, row){ 
								var str = "<a href=\"javascript:void(0)\" onclick=\"showCompInfo('"+row.compCode+"','"+row.prodPompName+"')\">"+cellvalue+"</a>";
								return str;
							}
						},
						{ name: 'lastyProAmt', width: 85, align: 'center' ,sortable: false},
						{ name: 'planProAmt', width: 85, align: 'center' ,sortable: false},
						{ name: 'proMethod', width: 80, align: 'center' ,sortable: false,
						    formatter:function (val,options,rowdata) {
								var str = "<select id=\"proMethod" + options.rowId + "\" >" + "<option selected=\"selected\" value=\"\">请选择</option>";
							    var catValues = new Array();
								var catTexts = new Array();
								
								<c:forEach var="proCat" items="${proCatList}" varStatus="status">;
								catValues["${status.index}"] = "${proCat.dicCode}";
								catTexts["${status.index}"] = "${proCat.dicName}";
							 </c:forEach>;

								for (var i = 0; i < catValues.length; ++i) {
									str += ("<option id=\"proMethod" + options.rowId + "\" name=\"proMethod\" value=\"" + catValues[i] + "\">" + catTexts[i] + "</option>");
								}
								str += "</select>";							 
							    return str;
							}
						},
						{ name: 'qualityRequires', width: 80, align: 'center' ,sortable: false}

	
						

                ],
            	gridComplete: function(){
            		var ids = jQuery("#gridlist").jqGrid('getDataIDs');
            		for(var i=0;i < ids.length;i++){
            			var cl = ids[i];
            			a = "<input type='text' name='lastyProAmt"+cl+"' id='lastyProAmt"+cl+"' maxlength='11'/>";
            			b = "<input type='text' name='planProAmt"+cl+"' id='planProAmt"+cl+"' maxlength='11'/>"; 
            			d = "<input type='text' name='qualityRequires"+cl+"' id='qualityRequires"+cl+"' maxlength='170'/>";
            			jQuery("#gridlist").jqGrid('setRowData',ids[i],{lastyProAmt:a,planProAmt:b,qualityRequires:d});            		
            		}
            		//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                    //数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
            	},
                rowNum: 20,
                rowList: [10, 20, 30,50,100],
                rownumbers: true,
                pager: "#gridpage",
                viewrecords: true,
                multiselect: true,
                caption: "${projName}  -  申报采购计划",
                jsonReader: {
                    repeatitems: false  
                },
                loadComplete: function (data) { //完成服务器请求后，回调函数
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            //$(window).trigger("resize");
        });
        function gridSerch() {
        	var drugCode = $.trim($("#drugCode").val());
        	var drugName = $("#drugName").val();
        	var compName = $("#compName").val();
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        	"drugCode":drugCode,
    	        	"drugName":drugName,
    	        		"compName":compName,
    	        		"projId":"${param.projId}",
    	        		"healbureaId":"${healbureaId}"
    	        }
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
        function batchDeclareDrug(){
        	var purProportion = Number("${purProportion==null?'0':purProportion}");
	    		var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
	    		if (rowIds.length <= 0) {
	    			$.HN.message.alert("请您选择要申报的药品！", "消息", "warn");
	    			return;
	    		}
	    		var list = [];
	    		var data = "";
	    		for (var i=0; i<rowIds.length; i++) {
	    			rowId = rowIds[i];
	    			var rowdata = $("#gridlist").jqGrid('getRowData', rowIds[i]);
	    			var id = rowIds[i];
	    			var lastyProAmt = $.trim($("#lastyProAmt"+id).val());
	            	if (lastyProAmt == ""){
	            		$.HN.message.alert('去年采购数量不能为空！', '提示信息', 'warn');
	            		return;
	            	}
	            	if (!new RegExp(/^[0-9]+$/).test(lastyProAmt)){
	            		$.HN.message.alert('去年采购数量必须是正整数！', '提示信息', 'warn');
	            		return;
	            	}
	            	if (lastyProAmt.length > 11 ) {
	                    $.HN.message.alert("去年采购数量长度不能超过11", "消息", "warn");
	                    return false;
	                }
	            	var planProAmt = $.trim($("#planProAmt"+id).val());
	            	if (planProAmt == ""){
	            		$.HN.message.alert('计划采购数量不能为空！', '提示信息', 'warn');
	            		return;
	            	}
	            	if (!new RegExp(/^[0-9]+$/).test(planProAmt)){
	            		$.HN.message.alert('计划采购数量必须是正整数！', '提示信息', 'warn');
	            		return;
	            	}
	            	if (planProAmt.length > 11 ) {
	                    $.HN.message.alert("计划采购数量长度不能超过11", "消息", "warn");
	                    return false;
	                }
	            	if(purProportion != 0 && (Number(lastyProAmt)*purProportion)>Number(planProAmt)){
	            		$.alert("计划采购数量不得低于去年的"+purProportion*100+"%","warn");
	            		return false;
	            	}
	            	var proMethod = $.trim($("#proMethod"+id).val());
	            	if (proMethod == ""){
	            		$.HN.message.alert('请选择采购方式！', '提示信息', 'warn');
	            		return;
	            	}
	            	var qualityRequires = $.trim($("#qualityRequires"+id).val());
	            	if (qualityRequires == ""){
	            		$.HN.message.alert('质量要求不能为空！', '提示信息', 'warn');
	            		return;
	            	}
	            	if (qualityRequires.length > 170 ) {
	                    $.HN.message.alert("质量要求长度不能超过170！请重新输入", "消息", "warn");
	                    return false;
	                }
	    			list.push({"lastyProAmt":lastyProAmt,
	            		"planProAmt":planProAmt,
	            		"proMethod":proMethod,
	            		"qualityRequires":qualityRequires,
	            		"projId":rowdata.projId,
	            		"healbureaId":"${healbureaId}",
	            		"drugCode":rowdata.drugId});
	    			data += "<br/>"+rowdata.drugName;
	    		}
	    		 $.HN.message.confirm("您确定申报以下药品吗？"+data, '', '').on(function (e) {
	                 if (e) {
			    		$.post("${pageContext.request.contextPath}/wsjplan/batchAddDrugData.html", {
			    			"array" : JSON.stringify(list)
			    		}, function(result) {
			    			if (result.success) {
			    				gridSerch();
			    				$.HN.message.alert("为您申报成功"+result.operCount+"条药品！", "消息", "success");
			    			} else {
			    				$.HN.message.alert("操作失败！", "消息", "error");
			    			}
			    		}, "json");
	                 }
	    		 });
        }
    </script>
</body>
</html>
